package com.example.chapter02;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.chapter02.service.MyServiceToStart;

import java.util.Random;

public class ActStartActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "ning";
    private static TextView tv_shu = null;
    private static Handler handler = new Handler();
    private static double randomDouble;


    @Override
    //创建活动
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"ActStartActivity onCreate");
        setContentView(R.layout.activity_act_start);

        findViewById(R.id.btn_act_next).setOnClickListener(this);

        Button btn_start = findViewById(R.id.btn_start);
        Button btn_end = findViewById(R.id.btn_end);
        tv_shu = findViewById(R.id.tv_shu);

        final Intent serviceIntent = new Intent(ActStartActivity.this,MyServiceToStart.class);
        btn_start.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View view){
                startService(serviceIntent);
            }
        });
        btn_end.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View view){
                stopService(serviceIntent);
            }
        });

    }


    public static void UpdateGUI(double refreshDouble){
        randomDouble = refreshDouble;
        handler.post(RefreshLable);
    }

    /**
     * Runnable对象中需要重载的run()函数，界面更新的代码在这里
     */
    private static Runnable RefreshLable = new Runnable() {
        @Override
        public void run() {
            tv_shu.setText(String.valueOf(randomDouble));
        }
    };



    @Override
    public void onClick(View v) {

                //1.在Intent的构造函数中指定
                /*Intent intent = new Intent(new Intent(this,ActFinishActivity.class));
                startActivity(intent);*/

                        //2.调用意图对象setClass方法指定
                /*Intent intent = new Intent();
                intent.setClass(this,ActFinishActivity.class);
                startActivity(intent);*/

                //3.调用意图对象setComponent方法指定
                Intent intent = new Intent();
                ComponentName component = new ComponentName(this,ActFinishActivity.class);
                intent.setComponent(component);
                startActivity(intent);


    }

    @Override
    //开始活动
    protected void onStart() {
        super.onStart();
        Log.d(TAG,"ActStartActivity onStart");
    }

    @Override
    //恢复活动
    protected void onResume() {
        super.onResume();
        Log.d(TAG,"ActStartActivity onResume");
    }

    @Override
    //暂停活动
    protected void onPause() {
        super.onPause();
        Log.d(TAG,"ActStartActivity onPause");
    }

    @Override
    //停止活动
    protected void onStop() {
        super.onStop();
        Log.d(TAG,"ActStartActivity onStop");
    }

    @Override
    //销毁活动
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"ActStartActivity onDestroy");
    }
}